<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.businessName" placeholder="关键词" clearable class="selWidth"/>
        </el-form-item>
        <el-form-item label="状态：">
          <el-select v-model="listPram.status" clearable style="width: 200px;" class="selWidth">
            <el-option label="在售" :value="1"></el-option>
            <el-option label="售罄" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <!--        <el-form-item class="el-form-item-align-right">-->
        <!--          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="openEdit()"-->
        <!--                     v-has-permi="['admin:commodity:save']">添加-->
        <!--          </el-button>-->
        <!--        </el-form-item>-->
      </el-form>
      <el-table :data="listData.records" size="mini"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column align="center" label="ID" prop="id" coverImg="120"></el-table-column>
        <el-table-column align="center" label="商家名称" prop="businessName" min-width="180"/>
        <el-table-column align="center" label="商家电话" min-width="130" prop="businessPhone"/>
        <el-table-column align="center" label="券额" prop="amount" min-width="130"/>
        <el-table-column align="center" label="出售时间" prop="saleTime" min-width="130"/>
        <el-table-column align="center" label="收款方式" min-width="150" prop="payType">
          <template slot-scope="scope">
            <div v-if="scope.row.payType===1">微信收款码</div>
            <div v-if="scope.row.payType===2">银行卡</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="80" prop="status">
          <template slot-scope="scope">
            <el-switch
                :disabled="!checkPermi(['admin:operate:hangingSale:sellOut'])"
                v-model="scope.row.status"
                :active-value="1"
                :inactive-value="0"
                active-text="在售"
                inactive-text="售罄"
                inactive-color="#ff4949"
                @change="handleStatusChange($event,scope.row.id)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="180" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" size="mini" type="success" @click="openEdit(scope.row.id)"
                       v-has-permi="['admin:operate:hangingSale:detail']">详情
            </el-button>
            <el-button icon="el-icon-delete" size="mini" type="danger" @click="handlerDelete(scope.row.id)"
                       v-has-permi="['admin:operate:hangingSale:delete']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :visible.sync="editDialog.show"
        title="详情"
        destroy-on-close
        :close-on-click-modal="false"
        width="1200px"
        top="55px">
      <edit
          v-if="editDialog.show"
          :id="editDialog.id"
          :imageUrl="imageUrl"
          @hideEditDialog="hideEditDialog"
          ref="editForm"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit.vue'
import {checkPermi} from "@/utils/permission";
import {list, updateStatus} from "@/api/hanging-sale"

export default {
  components: {edit},
  data() {
    return {
      listData: {records: [], total: 0},
      listPram: {
        businessName: '',
        status: '',
        page: 1,
        limit: 20,
      },
      editDialog: {
        show: false,
        id: null
      },
      tableLoading: false,
      imageUrl: '',
    }
  },
  mounted() {
    this.imageUrl = process.env.VUE_APP_IMAGE_URL
    this.getList()
  },
  methods: {
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        this.listData = res;
      }).finally(() => {
      })
      this.tableLoading = false;
    },
    openEdit(id = null) {
      this.editDialog.id = id;
      this.editDialog.show = true
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        updateStatus(id, 2).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    handleStatusChange(status, id) {
      const loading = this.$loading({text: '更新中', background: 'rgba(255,255,255,.5)'})
      updateStatus(id, status).then(() => {
        this.$message.success('更新状态成功')
        this.getList()
      }).finally(() => {
        loading.close()
      })
    },
    resetQuery() {
      this.listPram.businessName = '';
      this.listPram.status = '';
      this.value = '';
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">

</style>
